import cv2
import numpy as np

m_pic = cv2.imread("img/dog1.png")
h, w, _ = m_pic.shape
pic = cv2.imread("img/dogs.png")

# cv2.TM_SQDIFF_NORMED 差平方 --> 最小值
# cv2.TM_CCORR_NORMED,cv2.TM_CCOEFF_NORMED 乘积 --> 最大值
# result = cv2.matchTemplate(pic, m_pic, cv2.TM_SQDIFF_NORMED)
result = cv2.matchTemplate(pic, m_pic, cv2.TM_CCORR_NORMED)

minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
# 在实际的匹配算法中，需要根据匹配的算法，来决定取出最大值还是最小值坐标。
x1, y1 = maxLoc
cv2.rectangle(pic, (x1, y1), (x1 + w, y1 + h), (0, 0, 255), 2)
cv2.imshow("img",pic)
cv2.waitKey(0)
